<?php

class Users extends CI_Model {

    const Statustrue = True;
    const Statusflase = False;

    function __construct() {
        parent::__construct();
        $this->load->helper('fuc');
        $this->load->library('topclass');
        $this->load->library('session_db');
        $this->config->load('topapiconfig');
        //初始化类中变量
        $this->userinfo_array = $this->get_buyerinfo_array();
        $this->shopinfo_array = $this->get_shopinfo_array();
    }

    function get_userinfo_array() {// 根据昵称获取shoplogo //这里是卖家信息
        $top_session = $this->session_db->get_Sessiondb("top_session");
        $usernick = $this->session_db->get_Sessiondb("usernick");
        $res = $this->topclass->api_getUserGetRequest("UserSellerGetRequest", $usernick, $top_session);
        objectToArray(&$res);
        return $res;
    }

    function get_buyerinfo_array() {
        $top_session = $this->session_db->get_Sessiondb("top_session");
        $res = $this->topclass->api_UserBuyerGetRequest("UserBuyerGetRequest", $top_session);
        objectToArray(&$res);
        return $res;
    }

    function get_shopinfo_array() {//top授权处理流程
        $usernick = $this->session_db->get_Sessiondb("usernick");
        $res = $this->topclass->api_getShopGetRequest("ShopGetRequest", $usernick);
        objectToArray(&$res);
        return $res;
    }

    function get_appgroup_array() {//获取用户订购app对应系统中那个权限
    }

    function isget_userandshop_infoarray() {
        $userinfo_array = $this->userinfo_array;
        $shopinfo_array = $this->shopinfo_array;
        if (isset($userinfo_array["user"]) && isset($shopinfo_array["shop"])) {
            return Users::Statustrue;
        } else {
            return Users::Statusflase;
        }
    }

    function is_repeat_inUser($userinfo_array) {
        $sql = "select * from  user where  user_id=\"{$userinfo_array["user"]["user_id"]}\"";
        $query = $this->db->query($sql);
        $res = $query->result_array();
        if (empty($res)) {
            return Users::Statustrue;
        } else {
            return Users::Statusflase;
        }
    }

    function insert_user_table($userinfo_array, $shopinfo_array, $first_login) {
        echo $sql = "insert into user(nick,shop_id,user_id,
shop_logo,
first_login,
user_group)values(
\"{$userinfo_array["user"]["nick"]}\",
    \"{$shopinfo_array["shop"]["sid"]}\",\"{$userinfo_array["user"]["user_id"]}\",
     
            \"{$shopinfo_array["shop"]["pic_path"]}\",
           
                    \"{$first_login}\",0)";
        $query = $this->db->query($sql);
    }

    function update_user_table($userinfo_array, $shopinfo_array, $first_login) {
        echo $updatesql = "update user set shop_logo=\"{$shopinfo_array["shop"]["pic_path"]}\",
first_login=\"{$first_login}\",
               user_group=0  where  shop_id=\"{$shopinfo_array["shop"]["sid"]}\"   ";
        $this->db->query($updatesql);
    }

    function user_process_oauth() {//打算把这个在类内部拆解一下，为了减低些耦合性作尝试
        $userinfo_array = $this->userinfo_array;
        $shopinfo_array = $this->shopinfo_array;
//     

        if ($this->isget_userandshop_infoarray()) {//如果为真
//         
            $first_login = time();
            if ($this->is_repeat_inUser($userinfo_array)) {
                $this->insert_user_table($userinfo_array, $shopinfo_array, $first_login);//
                $this->session_db->write_Sessiondb("userid_my", $userinfo_array["user"]["user_id"]);
            } else {
                $this->update_user_table($userinfo_array, $shopinfo_array, $first_login);
                $this->session_db->write_Sessiondb("userid_my", $userinfo_array["user"]["user_id"]);
            }
        }
    }

}

?>
